const app = getApp()
const ipconfig = app.globalData.ipconfig;
Page({
    data: {
        currentIndex: 1, //默认是活动项
        showModalStatus: false,
        course: {
            title: '',
            introduce: '',
            picPath: '',
            videoPath: ''
        },
        str: ''
    },
    // 切换swiper-item触发bindchange事件
    pagechange: function (e) {
        // 通过touch判断，改变tab的下标值
        if ("touch" === e.detail.source) {
            let currentPageIndex = this.data.currentIndex;
            currentPageIndex = (currentPageIndex + 1) % 2;
            // 拿到当前索引并动态改变
            this.setData({
                currentIndex: currentPageIndex,
            })
        }
    },

    //点击tab时触发
    titleClick: function (e) {
        this.setData({
            //拿到当前索引并动态改变
            currentIndex: e.currentTarget.dataset.idx
        })
    },

    //文件上传
    uploadFile: function (e) {
        const that = this;
        console.log("that", that);
        wx.chooseMedia({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            mediaType: ['image', 'video'],
            maxDuration: 30,
            camera: 'back',
            success: function (res) {

                var tempFilePath = res.tempFiles[0].tempFilePath;
                wx.uploadFile({
                    url: 'http://' + ipconfig + ':8080/tool/upload',
                    filePath: tempFilePath,
                    name: 'file',
                    header: {
                        'Authorization': wx.getStorageSync('token')
                    },
                    success: function (res) {
                        console.log(res);
                        const data1 = JSON.parse(res.data)
                        if (data1.data.indexOf(".jpg") >= 0) {
                            that.setData({
                                'course.picPath': "http://rrjnomlap.hn-bkt.clouddn.com/" + data1.data
                            })
                        }
                        if (data1.data.indexOf(".mp4") >= 0) {
                            that.setData({
                                'course.videoPath': "http://rpv9gdn1y.hn-bkt.clouddn.com/" + data1.data
                            })
                        }
                        console.log(res);

                    },
                    fail: function (err) {
                        console.log(err);
                    }
                });

            },
            fail: function (err) {
                console.log(err);
            }
        })
    },


    commit(e) {
        console.log(e);
        console.log(this.data.course);
    },


    powerDrawer: function (e) {
        var currentStatu = e.currentTarget.dataset.statu;
        this.util(currentStatu)
    },

    util: function (currentStatu) {
        /* 动画部分 */
        // 第1步：创建动画实例
        var animation = wx.createAnimation({
            duration: 200, //动画时长
            timingFunction: "linear", //线性
            delay: 0 //0则不延迟
        });

        // 第2步：这个动画实例赋给当前的动画实例
        this.animation = animation;

        // 第3步：执行第一组动画
        animation.opacity(0).rotateX(-100).step();

        // 第4步：导出动画对象赋给数据对象储存
        this.setData({
            animationData: animation.export()
        })

        // 第5步：设置定时器到指定时候后，执行第二组动画
        setTimeout(function () {
            // 执行第二组动画
            animation.opacity(1).rotateX(0).step();
            // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
            this.setData({
                animationData: animation
            })
            console.log("211111111");
            //关闭
            if (currentStatu == "close") {
                this.setData({
                    showModalStatus: false
                });
            }
        }.bind(this), 200)

        // 显示
        if (currentStatu == "open") {
            this.setData({
                showModalStatus: true
            });
        }
    },

//无用
    pickerConfirm(e) {
        this.setData({
            pickerHidden: true,
            chosen: e.detail.value
        })
    },

    pickerCancel() {
        this.setData({
            pickerHidden: true
        })
    },

    pickerShow() {
        this.setData({
            pickerHidden: false
        })
    },

    formSubmit(e) {
        const value = e.detail.value;
        console.log(value);
        this.setData({
            'course.title': value.title,
            'course.introduce': value.introduce,
            chosen: ''
        })
        wx.request({
            url: 'http://' + ipconfig + ':8080/course/insert',
            data: this.data.course,
            method: 'POST',
            header: {
                'Authorization': wx.getStorageSync("token")
            },
            success(res) {
                console.log(res);
            },
            fail: function (err) {
                console.log(err);
            }
        })
        this.setData({
            'course.title': '',
            'course.introduce': '',
            'course.picPath': '',
            'course.videoPath': '',
        })
        console.log(this.data.course);

    },

    formReset(e) {
        console.log('form发生了reset事件，携带数据为：', e.detail.value)
        this.setData({
            chosen: ''
        })
        this.setData({
            'course.title': '',
            'course.introduce': '',
            'course.picPath': '',
            'course.videoPath': '',
        })
    }
})